package com.baibei.order.bill.orderrecord;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.baibei.model.BuyDirection;
import com.baibei.model.OrderInfo;
import com.baibei.module.OnItemSelectListener;
import com.baibei.order.R;
import com.baibei.order.R2;
import com.baibei.order.bill.sticky_header.StickyDateAdapter;
import com.baibei.order.bill.viewtype.ViewType;
import com.baibei.widget.PriceTextView;
import com.blankj.utilcode.utils.TimeUtils;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by shenminjie on 2017/8/9.
 * mail:shenminjie@100bei.com
 */

class OrderRecordAdapter extends StickyDateAdapter<OrderInfo> {

    /**
     * 回调操作
     */
    private OnItemSelectListener<OrderInfo> mListener;

    /**
     * 设置监听
     *
     * @param listener listener
     */
    void setOnListener(OnItemSelectListener<OrderInfo> listener) {
        mListener = listener;
    }

    @Override
    public RecyclerView.ViewHolder getItemViewHolder(ViewGroup parent, int viewType) {
        if (viewType == ViewType.ITEM.ordinal()) {
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_order_record, parent, false);
            return new ItemViewHolder(view);
        }
        return null;
    }

    @Override
    public void onBindItemViewHolder(RecyclerView.ViewHolder holder, final int position) {
        if (holder instanceof ItemViewHolder) {
            ItemViewHolder viewHolder = (ItemViewHolder) holder;
            final OrderInfo info = mRecordObjs.get(position);
            viewHolder.tvName.setText(info.getName());
            viewHolder.tvDate.setText(TimeUtils.millis2String(info.getAddtime(), "MM-dd"));
            viewHolder.tvAmount.setPrice(info.getPlamount());
            viewHolder.tvType.setText(info.getBuydirection() == BuyDirection.DIRECTION_CURRENT_PRICE ? R.string.current_price_order : R.string.settle_price_order);
            viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (mListener != null) {
                        mListener.onItemSelectListener(view, info, position);
                    }
                }
            });
        }
    }


    static class ItemViewHolder extends RecyclerView.ViewHolder {
        @BindView(R2.id.tv_name)
        TextView tvName;
        @BindView(R2.id.tv_amount)
        PriceTextView tvAmount;
        @BindView(R2.id.tv_date)
        TextView tvDate;
        @BindView(R2.id.tv_type)
        TextView tvType;

        ItemViewHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
        }
    }
}
